package com.xzd.util;

import java.lang.reflect.Method;

/**
 * java反射的相关实用方法
 * @author Administrator
 *
 */
public class ReflectUtils {
	
	/**
	 * 根据Bean的属性名称获取它的get方法·  如 property 获取方法名 getProperty()
	 * @param propertyName
	 * @return
	 * @throws NoSuchMethodException 
	 * @throws SecurityException 
	 */
	public static Method getGetMethod(String propertyName, Class<? extends Object> clazz) throws SecurityException, NoSuchMethodException {
		String methodName = "get" + propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1);
		
		return clazz.getMethod(methodName);
	}
	
	/**
	 * 根据Bean的属性名称获取它的set方法·  如 property 获取方法名 setProperty()
	 * @param propertyName
	 * @return
	 * @throws NoSuchMethodException 
	 * @throws SecurityException 
	 */
	public static Method getSetMethod(String propertyName, Class<? extends Object> clazz) throws SecurityException, NoSuchMethodException {
		String methodName = "set" + propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1);

		return clazz.getMethod(methodName);
	}
	
	/**
	 * 根据Bean的属性名称获取它的get方法·  如 property 获取方法名 getProperty()
	 * @param propertyName
	 * @return
	 * @throws NoSuchMethodException 
	 * @throws SecurityException 
	 */
	public static Method getGetMethod(String propertyName, Object obj) throws SecurityException, NoSuchMethodException {
		String methodName = "get" + propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1);
		
		return obj.getClass().getMethod(methodName);
	}
	
	/**
	 * 根据Bean的属性名称获取它的set方法·  如 property 获取方法名 setProperty()
	 * @param propertyName
	 * @return
	 * @throws NoSuchMethodException 
	 * @throws SecurityException 
	 */
	public static Method getSetMethod(String propertyName, Object obj) throws SecurityException, NoSuchMethodException {
		String methodName = "set" + propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1);

		return obj.getClass().getMethod(methodName);
	}
}
